//var  nconf = require('nconf');
////var  everyauth = require('everyauth');
//var  Recaptcha = require('recaptcha').Recaptcha;
//
//var usersById = {},
//    nextUserId = 0,
//    usersByLogin = {
//        'user@example.com': _addUser({ 
//            email: 'user@example.com',
//            password: 'azure'
//        })
//    };

/**
 * EVERYAUTH AUTHENTICATION
 * -------------------------------------------------------------------------------------------------
 * allows users to log in and register using OAuth services
 **/
    
/**
 * enable authentication for this application
 * @param fb :boolean Enable Facebook authentication
 * @param twit :boolean Enable Twitter authentication
 */
//function _enableAuth(fb, twit) {
//
//    everyauth.debug = true;
//
//    everyauth.
//    everymodule.
//    findUserById(function(id, callback) {
//        callback(null, usersById[id]);
//    });
//
//
//    /**
//     * FACEBOOK AUTHENTICATION
//     * -------------------------------------------------------------------------------------------------
//     * uncomment this section if you want to enable facebook authentication.  To use this, you will need
//     * to get a facebook application Id and Secret, and add those to settings.json.  See:
//     * http://developers.facebook.com/
//     **/
//
//    if (fb === true) {
//        var usersByFacebookId = {};
//        everyauth.
//        facebook.
//        appId(nconf.get('facebook:applicationId')).
//        appSecret(nconf.get('facebook:applicationSecret')).
//        findOrCreateUser(
//            function(session, accessToken, accessTokenExtra, fbUserMetadata) {
//                return usersByFacebookId[fbUserMetadata.claimedIdentifier] || (usersByFacebookId[fbUserMetadata.claimedIdentifier] = _addUser('facebook', fbUserMetadata));
//            }).redirectPath('/');
//    }
//
//    /**
//     * TWITTER AUTHENTICATION
//     * -------------------------------------------------------------------------------------------------
//     * uncomment this section if you want to enable twitter authentication.  To use this, you will need
//     * to get a twitter key and secret, and add those to settings.json.  See:
//     * https://dev.twitter.com/
//     **/
//
//    if (twit === true) {
//        var usersByTwitId = {};
//        everyauth.twitter.consumerKey(nconf.get('twitter:consumerKey')).consumerSecret(nconf.get('twitter:consumerSecret')).findOrCreateUser(function(sess, accessToken, accessSecret, twitUser) {
//            return usersByTwitId[twitUser.id] || (usersByTwitId[twitUser.id] = _addUser('twitter', twitUser));
//        }).redirectPath('/');
//    }
//
//    /**
//     * USERNAME & PASSWORD AUTHENTICATION
//     * -------------------------------------------------------------------------------------------------
//     * this section provides basic in-memory username and password authentication
//     **/
//
//    everyauth.password.loginWith('email').getLoginPath('/login').postLoginPath('/login').loginView('account/login').loginLocals(function(req, res, done) {
//        setTimeout(function() {
//            done(null, {
//                title: 'login.  '
//            });
//        }, 200);
//    }).authenticate(function(login, password) {
//        var errors = [];
//        if (!login) errors.push('Missing login');
//        if (!password) errors.push('Missing password');
//        if (errors.length) return errors;
//        var user = usersByLogin[login];
//        if (!user) return ['Login failed'];
//        if (user.password !== password) return ['Login failed'];
//        return user;
//    }).getRegisterPath('/register').postRegisterPath('/register').registerView('account/register').registerLocals(function(req, res, done) {
//        setTimeout(function() {
//            done(null, {
//                title: 'Register.  ',
//                recaptcha_form: (new Recaptcha(nconf.get('recaptcha:publicKey'), nconf.get('recaptcha:privateKey'))).toHTML()
//            });
//        }, 200);
//    }).extractExtraRegistrationParams(function(req) {
//        return {
//            confirmPassword: req.body.confirmPassword,
//            data: {
//                remoteip: req.connection.remoteAddress,
//                challenge: req.body.recaptcha_challenge_field,
//                response: req.body.recaptcha_response_field
//            }
//        }
//    }).validateRegistration(function(newUserAttrs, errors) {
//        var login = newUserAttrs.login;
//        var confirmPassword = newUserAttrs.confirmPassword;
//        if (!confirmPassword) errors.push('Missing password confirmation')
//        if (newUserAttrs.password != confirmPassword) errors.push('Passwords must match');
//        if (usersByLogin[login]) errors.push('Login already taken');
//
//        // validate the recaptcha 
//        var recaptcha = new Recaptcha(nconf.get('recaptcha:publicKey'), nconf.get('recaptcha:privateKey'), newUserAttrs.data);
//        recaptcha.verify(function(success, error_code) {
//            if (!success) {
//                errors.push('Invalid recaptcha - please try again');
//            }
//        });
//        return errors;
//    }).registerUser(function(newUserAttrs) {
//        var login = newUserAttrs[this.loginKey()];
//        return usersByLogin[login] = _addUser(newUserAttrs);
//    }).loginSuccessRedirect('/').registerSuccessRedirect('/');
//}

    // add a user to the in memory store of users.  If you were looking to use a persistent store, this
    // would be the place to start
function _addUser(source, sourceUser) {
        var user;
        if (arguments.length === 1) {
            user = sourceUser = source;
            user.id = ++nextUserId;
            return usersById[nextUserId] = user;
        } else { // non-password-based
            user = usersById[++nextUserId] = {
                id: nextUserId
            };
            user[source] = sourceUser;
        }
        return user;
    }
    
exports.enableAuth = _enableAuth; 
exports.addUser = _addUser; 